package com.zhisheng.orders.utils;

/*
    全哥
    我们自己基于官网手动封装的支付宝沙箱支付的工具类

*/

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.zhisheng.exception.MyBusinessException;
import com.zhisheng.orders.dto.AliPayDto;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "ali-pay")
public class MyAliPayUtils {

    private   String privateKey  ;
    private   String alipayPublicKey ;
    private   String serverUrl ;
    private   String appId ;
    private   String menDianId ;
    private   String returnUrl ;
    private   String notifyUrl ;
    public  String toAliPay(AliPayDto dto) throws AlipayApiException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();

        // 设置商户订单号
        model.setOutTradeNo(dto.getOrderId());

        // 设置订单总金额
        model.setTotalAmount(dto.getOrderMoney());

        // 设置订单标题
        model.setSubject(dto.getOrderSubject());

        // 设置产品码
        model.setProductCode("FAST_INSTANT_TRADE_PAY");

        // 设置PC扫码支付的方式
        //model.setQrPayMode("1");

        // 设置商户自定义二维码宽度
        //model.setQrcodeWidth(100L);

        // 设置订单包含的商品列表信息
        model.setGoodsDetail(dto.getGoodsDetail());

        // 设置订单绝对超时时间
       // model.setTimeExpire(dto.getTimeExpire());


        // 设置请求后页面的集成方式
        //model.setIntegrationType("PCWEB");


        // 设置商户门店编号
//        model.setStoreId(menDianId);
//
//        // 设置商户的原始订单号
//        model.setMerchantOrderNo(dto.getOrderId());


        request.setBizModel(model);
        request.setReturnUrl(returnUrl);
        request.setNotifyUrl(notifyUrl);

        // TODO 支付成功之后的同步回调地址
        // 支付成功之后的异步回调地址

        AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "POST");
        String pageRedirectionData = response.getBody();

        if (response.isSuccess()) {
            System.out.println("调用成功");
            return pageRedirectionData;
        } else {
            System.out.println("调用失败");
            throw new MyBusinessException("支付宝沙箱支付调用失败....");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }


    private  AlipayConfig getAlipayConfig() {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl(serverUrl);
        alipayConfig.setAppId(appId);
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }
}
